package com.lovely602.langchain4j.memory.controller;


import cn.hutool.core.date.DateUtil;
import com.lovely602.langchain4j.memory.service.ChatAssistant;
import com.lovely602.langchain4j.memory.service.ChatMemoryAssistant;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author lizhixing
 */
@Slf4j
@RestController
@RequestMapping("/chatmemory")
public class ChatMemoryController {

    @Resource
    private ChatAssistant chatAssistant;

    @Resource(name = "messageWindowChatMemoryAssistant")
    private ChatMemoryAssistant messageWindowChatMemoryAssistant;

    @Resource(name = "tokenWindowChatMemoryAssistant")
    private ChatMemoryAssistant tokenWindowChatMemoryAssistant;


    /**
     * 没有记忆缓存功能聊天
     */
    @GetMapping(value = "/chat01")
    public String chat() {

        String answer01 = chatAssistant.chat("你好，我的名字叫张三");
        System.out.println("answer01返回结果：" + answer01);

        String answer02 = chatAssistant.chat("我的名字是什么");
        System.out.println("answer02返回结果：" + answer02);

        return "success : " + DateUtil.now() + "<br> \n\n answer01: " + answer01 + "<br> \n\n answer02: " + answer02;
    }

    /**
     * MessageWindowChatMemory实现聊天功能
     */
    @GetMapping(value = "/chat02")
    public String chatMessageWindowChatMemory() {
        messageWindowChatMemoryAssistant.chatWithChatMemory(1L, "你好！我的名字是Java.");
        String answer01 = messageWindowChatMemoryAssistant.chatWithChatMemory(1L, "我的名字是什么");
        System.out.println("answer01返回结果：" + answer01);

        messageWindowChatMemoryAssistant.chatWithChatMemory(3L, "你好！我的名字是C++");
        String answer02 = messageWindowChatMemoryAssistant.chatWithChatMemory(3L, "我的名字是什么");
        System.out.println("answer02返回结果：" + answer02);

        return "chatMessageWindowChatMemory success : "
                + DateUtil.now() + "<br> \n\n answer01: " + answer01 + "<br> \n\n answer02: " + answer02;

    }

    /**
     * TokenWindowChatMemory实现聊天功能
     */
    @GetMapping(value = "/chat03")
    public String chatTokenWindowChatMemory() {
        tokenWindowChatMemoryAssistant.chatWithChatMemory(1L, "你好！我的名字是mysql");
        String answer01 = tokenWindowChatMemoryAssistant.chatWithChatMemory(1L, "我的名字是什么");
        System.out.println("answer01返回结果：" + answer01);

        tokenWindowChatMemoryAssistant.chatWithChatMemory(3L, "你好！我的名字是oracle");
        String answer02 = tokenWindowChatMemoryAssistant.chatWithChatMemory(3L, "我的名字是什么");
        System.out.println("answer02返回结果：" + answer02);

        return "chatTokenWindowChatMemory success : "
                + DateUtil.now() + "<br> \n\n answer01: " + answer01 + "<br> \n\n answer02: " + answer02;
    }


}
